package com.wwwarehouse.usercenter.fragment.permissiondistribute;

import android.os.Bundle;

import com.wwwarehouse.common.activity.base.BaseFragment;

/**
 * @author : mundane
 * @time : 2017/9/26 13:23
 * @description :
 * @file : BasePageFragment.java
 */

public abstract class BaseLazyFragment extends BaseFragment {

	protected boolean isViewInitiated;
	protected boolean isVisibleToUser;
	public boolean isDataInitiated;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		isViewInitiated = true;
		prepareFetchData();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		this.isVisibleToUser = isVisibleToUser;
		prepareFetchData();
	}

	public abstract void fetchData();

	public boolean prepareFetchData() {
		return prepareFetchData(false);
	}

	public boolean prepareFetchData(boolean forceUpdate) {
		if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
			fetchData();
			isDataInitiated = true;
			return true;
		}
		return false;
	}

}